package com.tecsup.log.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;


import com.tecsup.log.dao.MaterialesDAO;
import com.tecsup.excepcion.DAOExcepcion;
import com.tecsup.log.modelo.Materiales;


/**
 * Servlet implementation class MaterialesInsertarServlet
 */
@WebServlet("/MaterialesInsertarServlet")
public class MaterialesInsertarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MaterialesInsertarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		//String m = request.getParameter("id_grupo");
		//int n = Integer.parseInt(m);
		String x = request.getParameter("nombre");
		String a = request.getParameter("stock");
		Integer ax= Integer.parseInt(a);
		String b = request.getParameter("costoPromedioUnitario");
		Double bx= Double.parseDouble(b);
		String y = request.getParameter("descripcion");
		String c = request.getParameter("leadTime");
		Integer cx = Integer.parseInt(c);
		
		Materiales r = new Materiales();
		//r.setId_grupo(n);
		r.setNombre(x);
		r.setStock(ax);
		r.setCostoPromedioUnitario(bx);
		r.setDescripcion(y);
		r.setLeadTime(cx);
		
		MaterialesDAO dao = new MaterialesDAO();
		try {
			dao.insertar(r);
			response.sendRedirect(request.getContextPath()
					+ "/MaterialesBuscarServlet");
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}

	}
}
